package edu.ccut.saturn.component.loader.node;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;
import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.INodeInfo;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.impl.node.SaturnDataNodeInfo;

/**
 * 用于解析action组件及logic组件中的“数据结点”的加载类。
 * <p>
 * <code>NodeInfoLoader</code>,并实现了<code>loadNode</code>方法。
 * <p>
 * 
 * @author 温建
 * @version 1.0 2008.8.15
 * 
 * @see SaturnDataNodeInfo
 */
public class DataNodeInfoLoader extends NodeInfoLoader {

	public static final String KEY_NODETYPE_DATA = "data";

	public static final String KEY_DATA_SCOPE = "data-scope";

	public static final String KEY_DATA_PREFIX = "data-prefix";

	public static final String KEY_DATA_KEY = "data-key";

	public static final String KEY_DATA_TYPE = "data-type";

	public static final String KEY_DATA_TYPE_ATTRIBUTE_CLASS_TYPE = "classType";

	public static final String KEY_DATA_TYPE_ATTRIBUTE_NAMESPACE = "namespace";

	public static final String KEY_DATA_TYPE_ATTRIBUTE_TITTAN_DATA_TYPE = "saturnDataType";

	public static final String KEY_DATA_XMLKEY = "xmlkey";

	public static final String KEY_DATA_ELEMENTKEY = "elementkey";

	@SuppressWarnings("unchecked")
	public List<INodeInfo> loadNode(Element root, Bundle bundle)
			throws SaturnComponentException {

		List<Element> dataNodeElements = root.elements(KEY_NODETYPE_DATA);
		List<INodeInfo> dataNodeInfos = new ArrayList<INodeInfo>(
				dataNodeElements.size());

		for (Element dataNodeElement : dataNodeElements) {

			String id = dataNodeElement.attributeValue(KEY_NODE_ID).trim();
			String x = dataNodeElement.attributeValue(KEY_NODE_X).trim();
			String y = dataNodeElement.attributeValue(KEY_NODE_Y).trim();
			String nextId = dataNodeElement.attributeValue(KEY_NODE_NEXT)
					.trim();

			String name = dataNodeElement.elementTextTrim(KEY_NODE_NAME);
			String description = dataNodeElement
					.elementTextTrim(KEY_NODE_DESCRIPTION);

			String dataScope = dataNodeElement.elementTextTrim(KEY_DATA_SCOPE);
			String dataPrefix = dataNodeElement
					.elementTextTrim(KEY_DATA_PREFIX);

			String dataKey = dataNodeElement.elementTextTrim(KEY_DATA_KEY);
			Element dataType = dataNodeElement.element(KEY_DATA_TYPE);
			String xmlKey = dataNodeElement.elementText(KEY_DATA_XMLKEY);
			String elementKey = dataNodeElement
					.elementText(KEY_DATA_ELEMENTKEY);

			if (xmlKey != null && elementKey != null) {
				xmlKey = xmlKey.trim();
				elementKey = elementKey.trim();
			}

			String classType = null;
			String namespace = null;
			String saturnDataType = null;

			if (dataType != null) {
				classType = dataType
						.attributeValue(KEY_DATA_TYPE_ATTRIBUTE_CLASS_TYPE);

				namespace = dataType
						.attributeValue(KEY_DATA_TYPE_ATTRIBUTE_NAMESPACE);

				saturnDataType = dataType
						.attributeValue(KEY_DATA_TYPE_ATTRIBUTE_TITTAN_DATA_TYPE);
			}

			logger.debug("  load data node id = \"" + id + "\"name = \"" 
					+ name + "\" description = \"" + description
					+ "\" dataScope = \"" + dataScope + "\" dataPrefix = \"" 
					+ dataPrefix + "\" dataKey = \"" + dataKey 
					+ "\" classType = \"" + classType + "\" namespace = \""
					+ namespace + "\" saturnDataType = \"" + saturnDataType
					+ "\" xml key = \"" + xmlKey + "\" element key = \"" 
					+ elementKey + "\"");

			SaturnDataNodeInfo dataNodeInfo = new SaturnDataNodeInfo(id, name,
					description, nextId, dataScope, dataPrefix, dataKey,
					classType, namespace, saturnDataType, xmlKey, elementKey, x,
					y);

			dataNodeInfos.add(dataNodeInfo);
		}

		return dataNodeInfos;
	}

}
